import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    lists: JSON.parse(localStorage.getItem('lists')) || []
  },
  mutations: {
    addList(state, list) {
      state.lists.push({
        name: list.name,
        checked: list.checked,
        status: list.status
      })
      localStorage.lists = JSON.stringify(state.lists)
    },
    setChecked(state, item) {
      state.lists.forEach(list=> {
        if(list.name == item.name) {
          list.checked = item.checked
        }
        if(list.status != 'cancel') {
          if(list.checked) {
            list.status = 'finished'
          }else {
            list.status = 'pedding'
          }
        }else {
          return
        }
      })
      localStorage.lists = JSON.stringify(state.lists)
    },
    setCancel(state, name) {
      state.lists.forEach(list=> {
        if(list.name == name) {
          list.status = 'cancel'
        }
      })
      localStorage.lists = JSON.stringify(state.lists)
    }
  },
  actions: {
    getList(action, list) {
      action.commit('addList', list)
    },
    getChecked(action, item) {
      action.commit('setChecked', item)
    },
    getCancel(action, name) {
      action.commit('setCancel', name)
    }
  },
  modules: {
  }
})
